package com.itheima.health.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.common.MessageConst;
import com.itheima.health.dao.CheckItemDao;
import com.itheima.health.dao.CheckgroupDao;
import com.itheima.health.dao.GroupItemDao;
import com.itheima.health.exception.BusinessRuntimeException;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.pojo.GroupItem;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.result.PageResult;
import com.itheima.health.result.QueryPageBean;
import com.itheima.health.service.CheckgroupService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class CheckgroupServiceImpl implements CheckgroupService {
    @Autowired
    private CheckgroupDao checkgroupDao;
    @Autowired
    private CheckItemDao checkItemDao;
    @Autowired
    private GroupItemDao groupItemDao;
    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */

    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        log.info("[检查项-分页查询]data：{}",queryPageBean);
        //使用分页插件
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        // 分页查询数据库
        Page<CheckGroup> page = checkgroupDao.PageSelect(queryPageBean.getQueryString());
        //封装分页数据
        return new PageResult(page.getTotal(),page.getResult());
    }


    /**
     * 根据id查询检查组
     * @param id
     * @return
     */
    @Override
    public CheckGroup selectById(Integer id) {
        CheckGroup checkGroup = checkgroupDao.SelectById(id);
        return checkGroup;
    }

    /**
     * 根据检查组id查询检查项
     * @param id
     * @return
     */
    @Override
    public List<String> findCheckItemIdsByCheckGroupId(Integer id) {
        List<String> itemList = checkgroupDao.findCheckItemIdsByCheckGroupId(id);
        return itemList;
    }

    /**
     * 编辑检查项
     * @param list
     */
    @Override
    @Transactional
    public void edit(CheckGroup checkgroup,Integer[] list) {
        checkgroupDao.update(checkgroup);
        Integer id = checkgroup.getId();
        groupItemDao.deleteItemInGroupByGroupId(id);

        List<CheckItem> checkItems =new ArrayList<>();
        if (list != null && list.length != 0) {
            for (Integer l : list) {
                CheckItem checkItem = checkItemDao.selectById(l);
                GroupItem groupItem = new GroupItem();
                groupItem.setCheckGroupId(id);
                groupItem.setCheckItemId(checkItem.getId());
                BeanUtils.copyProperties(checkItem, groupItem);
                groupItemDao.insert(groupItem);
                checkItems.add(checkItem);
            }
            checkgroup.setCheckItems(checkItems);
        }else{
            throw new BusinessRuntimeException(MessageConst.ADD_CHECKGROUP_FAIL);
        }
    }

    /**
     * 新增检查组
     * @param checkGroup
     */
    @Override
    public void insert(CheckGroup checkGroup,Integer[] checkitemIds) {
        checkgroupDao.insert(checkGroup);

        Integer id = checkGroup.getId();
        List<CheckItem> checkItems =new ArrayList<>();
        if (checkitemIds != null && checkitemIds.length != 0) {
            for (Integer checkitemId : checkitemIds) {
                CheckItem checkItem = checkItemDao.selectById(checkitemId);
                GroupItem groupItem = new GroupItem();
                groupItem.setCheckGroupId(id);
                groupItem.setCheckItemId(checkItem.getId());
                BeanUtils.copyProperties(checkItem, groupItem);
                groupItemDao.insert(groupItem);
                checkItems.add(checkItem);
            }
            checkGroup.setCheckItems(checkItems);
        }else{
            throw new BusinessRuntimeException(MessageConst.ADD_CHECKGROUP_FAIL);
        }

    }

    /**
     * 根据id删除检查组
     * @param id
     */
    @Override
    public void delete(Integer id) {
        checkgroupDao.deleteById(id);

    }

    /**
     * 查询所有检查组
     * @return
     */
    @Override
    public List<CheckGroup> findAll() {
        log.info("[检查项-查询所有]~");
        return checkgroupDao.selectAll();
    }

}
